/*Lupton Teigen 2025 "Base Assumptions"
This script does two of the three empirical strategies: the Figure 1 
results and the "total proximity" Figure 4 results. 
The "Sixty-Mile Circles" that are represented by Figures 2 & 3 
are done elsewhere. Non-native Stata ADOs used: outreg2  		  
*/

clear
#delimit ;

/*pathname needs customization to find file 
created by "pool-recode-savedata script" */
use "E:\Dropbox\replicate_bases\ces06-22data_recoded.dta" ;
local workingcces e:\Dropbox\replicate_bases\cces_work\ ;

/* label fixing */
label variable logdistclosest "Logged Distance to Nearest Base" ;
label variable male "Male" ;
label variable inputstate_byte "State";
label variable zipblackperc "ZIP code Black %";
label variable ziphispperc "ZIP code Hispanic %";
label variable year "Year" ;

/*************************Bivariate comparisons************ */

ttest lowestdistance, by(goppid_l) ;
ttest lowestdistance, by(gop2ptyvote) ;

/*************************Two-Party Presidential Vote */

logit gop2ptyvote c.logdistclosest goppid_l dempid_l svyk whitenonhisp ba male vet evangc marrspouse i.bage zipblackperc ziphispperc i.dens_quartiles i.year i.inputstate_byte  ;
outreg2 using "`workingcces'tableappendix1.xls" , replace 2aster ctitle(Vote) bdec(2) excel lab;
margins, at(logdistclosest=(1 2 2.7 3.4 4.09));
#delimit ;
/*Making half of Figure 1 */

marginsplot, xdimension(logdist) recastci(rspike) ciopts(lwidth(vthin)) xscale(log) ytitle("Prob. Vote for GOP Candidate") ylabel(, angle(horizontal) format(%5.2f)) scheme(s1mono) title(" ") xtitle("Logged Distance") xlabel(1 "3" 2 "8" 2.7 "15" 3.4 "30" 4.09 "60") title("GOP President Vote 2008-2020", size(medium));
graph save "Graph" "`workingcces'vote-choice.gph", replace ;

/************************Party Identification */

logit goppid_l c.logdistclosest svyk whitenonhisp ba male vet evangc marrspouse i.bage zipblackperc ziphispperc i.dens_quartiles i.year i.inputstate_byte ;
outreg2 using "`workingcces'tableappendix1.xls" , append 2aster ctitle(GOP) bdec(2) excel lab;
margins, at(logdistclosest=(1 2 2.7 3.4 4.09)) ;

/*Making second half of Figure 1*/
marginsplot, xdimension(logdist) recastci(rspike) ciopts(lwidth(vthin)) xscale(log) ytitle("Prob. of GOP PID or Lean") ylabel(, angle(horizontal) format(%5.2f)) scheme(s1mono) title(" ") xtitle("Logged Distance") xlabel(1 "3" 2 "8" 2.7 "15" 3.4 "30" 4.09 "60") title("GOP PID or Lean 2006-2022", size(medium));
graph save "Graph" "`workingcces'pid.gph", replace ;

/*Making combined Figure 1*/
#delimit ;

graph combine `workingcces'vote-choice.gph `workingcces'pid.gph , row(1) ycommon ;

/* Sixty-Mile Circles: See other scripts */



/* GOP PID for "total proximity" model & figure 4 & appendix table 3  */

logit goppid_l roundedlots svyk whitenonhisp ba male vet evangc marrspouse i.bage zipblackperc ziphispperc i.dens_quartiles i.year 
i.inputstate_byte  ;
outreg2 using "`workingcces'tableappendix3.xls" , replace 2aster ctitle(Vote) bdec(2) excel lab;

margins, over(roundedlots);
#delimit ;
marginsplot, 
	xdimension(roundedlots) 
	recastci(rspike) 
	ciopts(lwidth(vthin)) 
	ylabel(, angle(horizontal) format(%5.2f))   
	ytitle(" ")
	scheme(s1mono) 
	title(" ") 
	xtitle("Proximity") 
	text(.1 10 "Far", size(small))
	text(.1 600 "Near", size(small))
	title("GOP & GOP Leaning PID likelihood 2006-2022", size(medium))
	note("Sample size: `r(N)'")
;
graph save "Graph" "`workingcces'total_goppid.gph", replace ;
#delimit ;

/* GOP pres vote voice for "total proximity" model */

logit gop2ptyvote roundedlots goppid_l dempid_l svyk whitenonhisp ba male vet evangc marrspouse i.bage zipblackperc ziphispperc i.dens_quartiles i.year i.inputstate_byte  ;
outreg2 using "`workingcces'tableappendix3.xls"  , append 2aster ctitle(Vote) bdec(2) excel lab;

margins, over(roundedlots);
#delimit ;
marginsplot, 
	xdimension(roundedlots) 
	recastci(rspike) 
	ciopts(lwidth(vthin)) 
	ylabel(, angle(horizontal) format(%5.2f)) 
	scheme(s1mono) 
	title(" ") 
	ytitle(" ")
	xtitle("Proximity") 
	text(.1 10 "Far", size(small))
	text(.1 600 "Near", size(small))
	title("Voting GOP in 2008 2012 2016 or 2020", size(medium))
	note("Sample size: `r(N)'")
;
graph save "Graph" "`workingcces'total_gopvote.gph", replace ;


/* Making Figure 4 */
graph combine `workingcces'total_goppid.gph `workingcces'total_gopvote.gph ,
	row(1)
	ycommon ;

graph export "`workingcces'Figure_4.tiff", replace width(2100) as(tif);